﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Evan.WebUI
{
    public class ListManager : ManagerPage
    {
        protected string TableName = "";

        protected Evan.Model.SysModel.PageModels List = new Model.SysModel.PageModels();
        protected List<dynamic> Lists = new List<dynamic>();
        protected int PageSize = Convert.ToInt32(Evan.Common.CommFun.GetAppSetting("Admin_PageSize"));
        protected int CurPage = 1;
        protected string key = "";
        
        public ListManager()
        {
            this.Load += new EventHandler(ListManager_Load);
        }

        private void ListManager_Load(object sender, EventArgs e)
        {
            TableName = Common.CommFun.GetQueryString("t");
            int.TryParse(Common.CommFun.GetQueryString("page"), out CurPage);
            int.TryParse(Common.CommFun.GetQueryString("classid"), out ClassId);
            string where = " States>-1 ";
            key = Common.CommFun.GetQueryString("key");
            if (!string.IsNullOrEmpty(key))
            {
                where += string.Format("and (Title like '%{0}%') ", key);
            }

            if (string.IsNullOrEmpty(TableName))
            {
                throw new Exception("参数 t 不能为空");
            }

            string[] ArrTable = { "Channel", "Administrator", "Areas", "Roles", "Versions", "Fields" }; //部分系统表不允许有分页
            if (ArrTable.Contains(TableName)) //系统表
            {
                if (TableName == "Channel") //栏目
                {
                    Lists = new Evan.DataBase.DbOperat(TableName).GetList(where, "Id Asc");
                }
                else if (TableName == "Fields") //字段管理
                {
                    string Field_ClassId = Common.CommFun.GetQueryString("ClassId");
                    if (string.IsNullOrEmpty(Field_ClassId))
                    {
                        throw new Exception("字段管理中 ClassId 参数不能为空");
                    }
                    where += string.Format(" and ClassId={0} ", Field_ClassId);
                    Lists = new Evan.DataBase.DbOperat(TableName).GetList(where, "SortId asc,Id Asc");
                }
                else
                {
                    Lists = new Evan.DataBase.DbOperat(TableName).GetList(where, "SortId asc,Id Asc");
                }
            }
            else
            {
                if (ClassId > 0)
                {
                    if (TableName == "Module") //栏目
                    {
                        where += " and ChannelId=" + ClassId;
                    }
                    else
                    {
                        where += " and ClassId=" + ClassId;
                    }
                    //string curChannelOrderBy = Template.TemplateHelper.GetSqlOrderStr(Convert.ToInt32(CurChannel.OrderBy), Convert.ToInt32(CurChannel.SubOrderBy));
                    string curChannelOrderBy = Template.TemplateHelper.GetSqlOrderStr(CurChannel);

                    //Evan.SystemLog.MemLog.AddExLog2("后台排序", null, curChannelOrderBy);

                    //List = new Evan.DataBase.DbOperat(TableName).GetPage(PageSize, CurPage, where, "SortId Desc,Id Desc"); //bll.Page(PageSize, CurPage > 0 ? CurPage : 1, where, " id desc");
                    List = new Evan.DataBase.DbOperat(TableName).GetPage_2(PageSize, CurPage, where, curChannelOrderBy, false); //bll.Page(PageSize, CurPage > 0 ? CurPage : 1, where, " id desc");    
                }
                else
                {
                    List = new Evan.DataBase.DbOperat(TableName).GetPage_2(PageSize, CurPage, where, "Id Desc", false);
                }

            }


        }
    }
}
